Linear Search in Python
Linear Search is used to search an item in a list or array using linear traversal and comparison with each and every element.
Best Case : O(1)
Worst Case: O(n)
linearsearch.py
# [1.1] Linear Search arr = [3,6,12,65,2,9] #list / array item = 12 for i in range(len(arr)): if item == arr[i]: print(arr[i],i) # Find number of occurences in a given string str = "thinkx academy" element = 'a' count = 0 for i in range(len(str)): if element == str[i]: count = count + 1 print(count)
Output
12 2 2